package webshop.controller.action;

import java.util.ArrayList;
import java.util.List;

import webshop.model.bl.CategoryManager;
import webshop.model.bl.ProductManager;
import webshop.model.db.Category;
import webshop.model.db.Product;

import com.opensymphony.xwork2.ActionSupport;

public class LoadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3217152965366446315L;
	
	private List<Category> categoryList = new ArrayList<Category>();
	private List<Product> productList = new ArrayList<Product>();
	
	private Boolean productDataPosted;
	private Boolean categoryDataPosted;
	
	private String name;
	private String description;
	private Integer stock;
	private Double price;
	private Integer deliveryTime;
	private Integer categoryId;
	private String categoryname;
	
	public void validate() {}
	
	public String execute() {
		System.out.println("executed");
		productDataPosted = productDataPosted == null ? false : true;
		categoryDataPosted = categoryDataPosted == null ? false : true;
		if (productDataPosted) {
			validateProductData();
		}
		if (categoryDataPosted) {
			validateCategoryData();
		}
		
		try {
			categoryList = new CategoryManager().getAllCategories();
			productList = new ProductManager().getAllProducts();
		} catch (Throwable ex) {
			// TODO add error msg
		}
		
		if (hasActionErrors() || hasFieldErrors() || (!productDataPosted && !categoryDataPosted)) {
			System.out.println("returning input");
			return INPUT;
		}
		return SUCCESS;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}
	
	public List<Product> getProductList() {
		return productList;
	}
	
	private void validateCategoryData() {
		if ("".equals(categoryname)) {
			addFieldError("categoryname", "");
			addActionError(getText("error.empty.fields"));
		} else if (new CategoryManager().categoryExists(categoryname)) {
			addFieldError("categoryname", "");
			addActionError("Die Kategorie '" + categoryname + "' existiert bereits.");
		}
	}

	private void validateProductData() {
		boolean emptyFields = false;
		if (categoryId == null  || categoryId == -1) {
			addFieldError("categoryId", "");
			emptyFields = true;
		}
		if ("".equals(name)) {
			addFieldError("name", "");
			emptyFields = true;
		} else if (new ProductManager().productExists(name)) {
			addFieldError("name", "");
			addActionError("Das Produkt '" + name + "' existiert bereits.");
		}
		if ("".equals(description)) {
			emptyFields = true;
			addFieldError("description", "");
		}
		if (stock == null) {
			addFieldError("stock", "");
			emptyFields = true;
		}
		if (price == null) {
			addFieldError("price", "");
			emptyFields = true;
		}
		if (deliveryTime == null) {
			addFieldError("deliveryTime", "");
			emptyFields = true;
		}
		if (emptyFields) {
			addActionError(getText("error.empty.fields"));
		}
	}

	public Boolean getProductDataPosted() {
		return productDataPosted;
	}

	public void setProductDataPosted(Boolean dataPosted) {
		this.productDataPosted = dataPosted;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Integer getStock() {
		return stock;
	}
	public void setStock(String stock) {
		try {
			this.stock = Integer.parseInt(stock);
		} catch (Exception e) {
			this.stock = null;
		}
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(String price) {
		price = price.replace(",", ".");
		try {
			this.price = Double.parseDouble(price);
		} catch (Exception ex) {
			this.price = null;
		}
	}
	public Integer getDeliveryTime() {
		return deliveryTime;
	}
	public void setDeliveryTime(String deliveryTime) {
		try {
			this.deliveryTime = Integer.parseInt(deliveryTime);
		} catch (Exception ex) {
			this.deliveryTime = null;
		}
	}
	public String getCategoryId() {
		return "" + categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = null;
		try {
			this.categoryId = Integer.parseInt(categoryId);
		} catch (Throwable ex) {}
	}

	public String getCategoryname() {
		return categoryname;
	}

	public void setCategoryname(String categoryname) {
		this.categoryname = categoryname;
	}

	public Boolean getCategoryDataPosted() {
		return categoryDataPosted;
	}

	public void setCategoryDataPosted(Boolean categoryDataPosted) {
		this.categoryDataPosted = categoryDataPosted;
	}

}
